package com.lkd.handler;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.OrderCheck;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @Author ChenChangYu
 * @Date 2022 09 22 18 17
 **/
@Component
@Topic(TopicConfig.ORDER_CHECK_TOPIC)
public class OrderCheckHandler implements MsgHandler {

    @Autowired
    private OrderService orderService;

    /**
     * 超时取消订单
     * @param jsonMsg
     * @throws IOException
     */
    @Override
    public void process(String jsonMsg) throws IOException {
        if (StringUtils.isBlank(jsonMsg)){
            return;
        }
        //解析消息
        OrderCheck orderCheck = JsonUtil.getByJson(jsonMsg, OrderCheck.class);
        if (orderCheck == null){
            return;
        }

        //查询订单
        OrderEntity orderEntity = orderService.getByOrderNo(orderCheck.getOrderNo());
        //订单超时未支付则取消
        if (VMSystem.ORDER_STATUS_CREATE.equals(orderEntity.getStatus())){
            orderEntity.setPayStatus(VMSystem.ORDER_STATUS_INVALID);
        }

        orderService.updateById(orderEntity);
    }
}




















